package com.example.workroom.service.impl;
import com.example.workroom.dto.PeoplePageQueryDTO;
import com.example.workroom.mapper.PeopleMapper;
import com.example.workroom.pojo.People;
import com.example.workroom.result.PageResult;
import com.example.workroom.result.Result;
import com.example.workroom.service.PeopleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PeopleServiceImpl implements PeopleService {

    @Autowired
    private PeopleMapper peopleMapper;
    /**
     * 新增成员
     * @param people
     */
    @Override
    public void save(People people) {
        //设置当前记录的创建时间和修改时间
        people.setCreateTime(LocalDateTime.now());
        people.setUpdateTime(LocalDateTime.now());
        peopleMapper.insert(people);
    }

    /**
     * 分页查询
     * @param peoplePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(PeoplePageQueryDTO peoplePageQueryDTO) {
        // 启动分页功能
        PageHelper.startPage(peoplePageQueryDTO.getPage(), peoplePageQueryDTO.getPageSize());

        // 执行分页查询
        Page<People> page = peopleMapper.pageQuery(peoplePageQueryDTO);

        // 获取总记录数
        long total = page.getTotal();

        // 获取当前页的记录列表
        List<People> records = page.getResult();

        // 封装分页结果并返回
        return new PageResult(total, records);


    }

    /**
     * 批量删除员工数据
     * @param ids
     */
    @Override
    public void deleteIds(List<Integer> ids) {
        peopleMapper.deleteIds(ids);
    }

    /**
     * 根据id 查询员工
     * @param id
     * @return
     */
    @Override
    public People getById(Integer id) {
        People people=peopleMapper.getById(id);
        return people;
    }

    /**
     * 根据id 修改员工信息
     * @param people
     */
    @Override
    public void update(People people) {
        //改变更新时间
        people.setUpdateTime(LocalDateTime.now());

        peopleMapper.update(people);
    }


}
